import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '@/components/LayoutContainer.vue'
import Nprogress from '../utils/nprogress'

Nprogress.configure({ showSpinner: false })

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      meta: {
        title: '首页'
      },
      redirect: '/welcome',
      component: HomeView,
      children: [
        {
          path: '/welcome',
          component: () => import('@/views/welcome/Welcome.vue'),
          meta: {
            title: '欢迎首页'
          }
        },
        {
          path: 'system/index',
          component: () => import('@/views/index/index.vue'),
          meta: {
            title: '系统首页'
          }
        },
        {
          path: 'system/user',
          name: 'user',
          meta: {
            title: '员工管理'
          },
          component: () => import('@/views/user/UserView.vue')
        },
        {
          path: 'system/userInfo',
          name: 'userInfo',
          meta: {
            title: '基本资料'
          },
          component: () => import('@/views/userInfo/index.vue')
        },
        {
          path: 'system/menu',
          name: 'menu',
          meta: {
            title: '菜单管理'
          },
          component: () => import('@/views/menu/MenuView.vue')
        },
        {
          path: 'system/role',
          name: 'role',
          meta: {
            title: '角色管理'
          },
          component: () => import('@/views/role/RoleView.vue')
        },
        {
          path: 'system/canlendar',
          name: 'canlendar',
          meta: {
            title: '日志管理'
          },
          component: () => import('@/views/fullcalendar/Fullcalendar.vue')
        },
        {
          path: 'system/dept',
          name: 'dept',
          meta: {
            title: '部门管理'
          },
          component: () => import('@/views/dept/DeptView.vue')
        },
        {
          path: 'system/notice',
          name: 'notice',
          meta: {
            title: '公告管理'
          },
          component: () => import('@/views/notice/NoticeView.vue')
        },
        {
          path: 'system/socket',
          name: 'socket',
          meta: {
            title: '工作聊天室'
          },
          component: () => import('@/views/socket/index.vue')
        },
        {
          path: 'apply/leave',
          name: 'leave',
          meta: {
            title: '休假申请'
          },
          component: () => import('@/views/leave/LeaveView.vue')
        },
        {
          path: 'apply/approve',
          name: 'approve',
          meta: {
            title: '待我审批'
          },
          component: () => import('@/views/approve/ApproveView.vue')
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('@/views/login/LoginPage.vue')
    },
    {
      name: '404',
      path: '/404',
      meta: {
        title: '页面不存在'
      },
      component: () => import('@/components/404.vue')
    }
  ]
})

// router.addRoute('home', {
//   name: 'menu',
//   path: 'system/menu',
//   meta: {
//     title: '菜单管理'
//   },
//   component: () => import('@/views/menu/MenuView.vue')
// })

router.beforeEach((to, from, next) => {
  Nprogress.start()
  document.title = to.meta.title + '-后台管理系统' || '默认标题'
  next()
})
router.afterEach((to, from) => {
  Nprogress.done()
})
export default router
